package com.shop.admin.controller;

import com.shop.admin.result.ApiResult;
import com.shop.admin.service.ShopOrderService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.valid.AddGroup;
import com.shop.admin.valid.UpdateGroup;
import com.shop.admin.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api(tags = {"ShopOrderController"},description = "零食商品接口")
@RestController
@RequestMapping("/api/order")
public class ShopOrderController {

    @Resource
    private ShopOrderService shopOrderService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "商品列表,条件查询",notes = "page,limit,key封装商品id或商品名字，分类id,商品状态,学校下楼栋id(orgId)")
    public ApiResult list( @ApiParam("参数为：page,limit,key,catalogId,showStatus,orgId")@RequestParam Map<String, Object> params){
        PageUtils page = shopOrderService.queryPage(params);
        return ApiResult.ok(page);
    }

//    //条件查询
//    @RequestMapping("/condition")
//    @ApiOperation(value = "条件查询",notes = "key封装商品id和商品名字，分类id,商品状态")
//    public ApiResult condition(@ApiParam("参数为：key")@RequestParam Map<String, Object> params){
//        PageUtils page = shopOrderService.queryPageByCondition(params);
//        return ApiResult.ok(page);
//    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "商品新增",notes = "商品名字，分类id，状态，价格，优惠价，成本价，库存，商品描述，图片集,学校下楼栋id(orgId)")
    public ApiResult save(@Validated(AddGroup.class) @ApiParam("参数为：orderName,catelogId,showStatus,price,priceDiscounts，priceCost，stockCount,orderDescribed,imagesList[imgUrl,defaultImg]，orgId") @RequestBody OrderVo vo){
        if (shopOrderService.selectBoolean(vo.getOrderName(),vo.getOrgId())){
            shopOrderService.saveOrderList(vo);
            return ApiResult.ok("新增成功！");
        }
        return ApiResult.ok("商品名重复");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "商品修改",notes = "商品名字，分类名字，状态，价格，优惠价，成本价,库存，商品描述,图片集")
    public ApiResult update(@ApiParam("参数为：orderId,orderName,catelogId,showStatus,price,stockCount，orderDescribed,imagesList[imgUrl,defaultImg]")@Validated(UpdateGroup.class) @RequestBody OrderListVo vo){
        shopOrderService.updateDetail(vo);
        return ApiResult.ok("修改成功！");
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "商品删除",notes = "商品id数组")
    public ApiResult delete(@ApiParam("参数为：orderId")@RequestBody Long[] ids){
        shopOrderService.removeMenuByIds(Arrays.asList(ids));
        return ApiResult.ok("删除成功！");
    }

    /**
     * 商品标签查找
     */
    @GetMapping("/taglist")
    @ApiOperation(value = "get商品标签查找",notes = "商品id")
    public ApiResult tagList(@ApiParam("参数为：orderId")@RequestParam("orderId") Long orderId){
        if (orderId!=null){
            List<TagListVo> vos= shopOrderService.selectTagById(orderId);
            if (vos!=null||vos.size()==0){
                return ApiResult.ok(vos);
            }
            else return ApiResult.ok(null);
        }
       return ApiResult.ok("请传递商品id");
    }

    /**
     * 商品标签修改and新增
     */
    @PostMapping("/tagupdate")
    @ApiOperation(value = "商品标签修改and新增",notes = "id,商品id,标签集")
    public ApiResult tagUpdate(@ApiParam("参数为：orderId,vos(Id,List<tagKey,tagValue>)")@RequestBody UpdateTagListVo listVO){
        shopOrderService.updateTagList(listVO);
        return ApiResult.ok("操作成功！");
    }

    /**
     * 商品标签删除
     */
    @PostMapping("/tagdelete")
    @ApiOperation(value = "商品标签删除",notes = "ids")
    public ApiResult tagDelete(@ApiParam("参数为：id[]")@RequestBody Long[] ids){
        shopOrderService.deleteTags(Arrays.asList(ids));
        return ApiResult.ok("删除成功！");
    }

    /**
     * 秒杀状态设置
     */
    @PostMapping("/seckillstatus")
    @ApiOperation(value = "设置商品秒杀状态",notes = "orderId,seckill")
    public ApiResult seckill(@ApiParam("参数为：商品id,秒杀状态(0为普通商品,1为秒杀商品)")@RequestBody SeckillStatusVo statusVO){
        shopOrderService.seckillStatus(statusVO);
        return ApiResult.ok("状态修改成功！");
    }
}
